<!DOCTYPE html>
<html lang="en">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/old-blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/old-blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/old-blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/old-blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/old-blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhzhzhy.github.io","root":"/old-blog/","images":"/old-blog/images","scheme":"Pisces","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="vue-tetris 使用Vue实现的俄罗斯方块游戏  Build Setup12345678# install dependenciesnpm install# serve with hot reload at localhost:8080npm run dev# build for production with minificationnpm run build  For detailed">
<meta property="og:type" content="article">
<meta property="og:title" content="网页版 俄罗斯方块tetris">
<meta property="og:url" content="https://zhzhzhy.github.io/old-blog/2018/03/01/tetris/index.html">
<meta property="og:site_name" content="ZhZhY的博客 | ZhZhY&#39;s">
<meta property="og:description" content="vue-tetris 使用Vue实现的俄罗斯方块游戏  Build Setup12345678# install dependenciesnpm install# serve with hot reload at localhost:8080npm run dev# build for production with minificationnpm run build  For detailed">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://olnzpdi2u.bkt.clouddn.com/3908048923-57dc200ce044c_articlex.png">
<meta property="article:published_time" content="2018-03-01T17:49:24.000Z">
<meta property="article:modified_time" content="2021-04-15T12:14:56.264Z">
<meta property="article:author" content="ZhZhy">
<meta property="article:tag" content="技术分享">
<meta property="article:tag" content="vue">
<meta property="article:tag" content="github">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://olnzpdi2u.bkt.clouddn.com/3908048923-57dc200ce044c_articlex.png">


<link rel="canonical" href="https://zhzhzhy.github.io/old-blog/2018/03/01/tetris/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>网页版 俄罗斯方块tetris | ZhZhY的博客 | ZhZhY's</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
<!-- hexo injector head_end end --><link rel="alternate" href="/old-blog/atom.xml" title="ZhZhY的博客 | ZhZhY's" type="application/atom+xml"><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/old-blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">ZhZhY的博客 | ZhZhY's</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">愿你出走半生，归来仍是少年</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-主页-home"><a href="/old-blog/" rel="section"><i class="fa fa-home fa-fw"></i>主页 Home</a></li>
        <li class="menu-item menu-item-关于我-about"><a href="/old-blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我 About</a></li>
        <li class="menu-item menu-item-相册-photo"><a href="https://zhzhy.tuchong.com/" rel="noopener" target="_blank"><i class="fa fa-picture-o fa-fw"></i>相册 Photo</a></li>
        <li class="menu-item menu-item-标签-tags"><a href="/old-blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签 Tags</a></li>
        <li class="menu-item menu-item-分类-category"><a href="/old-blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类 Category</a></li>
        <li class="menu-item menu-item-回望我的过去-archives"><a href="/old-blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>回望我的过去 Archives</a></li>
        <li class="menu-item menu-item-小工具-tools"><a href="/old-blog/tools/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>小工具 Tools</a></li>
        <li class="menu-item menu-item-sitemap"><a href="/old-blog/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>Sitemap</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#vue-tetris"><span class="nav-number">1.</span> <span class="nav-text">vue-tetris</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Build-Setup"><span class="nav-number">1.1.</span> <span class="nav-text">Build Setup</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9C%A8%E7%BA%BF%E8%AE%BF%E9%97%AE"><span class="nav-number">1.2.</span> <span class="nav-text">在线访问</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81"><span class="nav-number">1.3.</span> <span class="nav-text">项目源码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%B8%E6%88%8F%E7%AE%80%E4%BB%8B"><span class="nav-number">1.4.</span> <span class="nav-text">游戏简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%93%8D%E4%BD%9C%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.5.</span> <span class="nav-text">操作介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%AE%E7%9B%98%E6%93%8D%E4%BD%9C"><span class="nav-number">1.5.1.</span> <span class="nav-text">键盘操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%8F%E5%B9%95%E6%93%8D%E4%BD%9C"><span class="nav-number">1.5.2.</span> <span class="nav-text">屏幕操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84"><span class="nav-number">1.6.</span> <span class="nav-text">项目结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%80%9D%E8%B7%AF"><span class="nav-number">1.7.</span> <span class="nav-text">实现思路</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%86%E5%9B%BE%E9%83%A8%E5%88%86"><span class="nav-number">1.7.1.</span> <span class="nav-text">视图部分</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E5%9D%97%E6%A8%A1%E5%9E%8B"><span class="nav-number">1.7.2.</span> <span class="nav-text">方块模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%8B%E8%BD%AC%E7%AE%97%E6%B3%95"><span class="nav-number">1.7.3.</span> <span class="nav-text">旋转算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A2%B0%E6%92%9E%E7%AE%97%E6%B3%95"><span class="nav-number">1.7.4.</span> <span class="nav-text">碰撞算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E5%92%8C%E5%8F%98%E5%BD%A2"><span class="nav-number">1.7.5.</span> <span class="nav-text">移动和变形</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">1.8.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="ZhZhy"
      src="/old-blog/assets/img/avatar.jpg">
  <p class="site-author-name" itemprop="name">ZhZhy</p>
  <div class="site-description" itemprop="description">geek猫，中国科大14级，南七技校工科男一枚。不知名博主，擅长linux运维，爱好摄影，歌风民谣，专业力学</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/old-blog/archives">
          <span class="site-state-item-count">74</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/old-blog/categories/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/old-blog/tags/">
        <span class="site-state-item-count">67</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/zhzhzhy" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhzhzhy" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:by6dx1@gmail.com" title="E-Mail → mailto:by6dx1@gmail.com" rel="noopener" target="_blank"><i class="fab fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://instagram.com/geek.cat.zzy" title="Instagram → https:&#x2F;&#x2F;instagram.com&#x2F;geek.cat.zzy" rel="noopener" target="_blank"><i class="fab fa-instagram fa-fw"></i>Instagram</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://open.spotify.com/artist/2fbUQIKSBTohTK6tHaFGYV?si=A2OzzFvPTjOVYcHiilCOWA" title="Spotify → https:&#x2F;&#x2F;open.spotify.com&#x2F;artist&#x2F;2fbUQIKSBTohTK6tHaFGYV?si&#x3D;A2OzzFvPTjOVYcHiilCOWA" rel="noopener" target="_blank">Spotify</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://zhzhzhy.github.io/old-blog/2018/03/01/tetris/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/old-blog/assets/img/avatar.jpg">
      <meta itemprop="name" content="ZhZhy">
      <meta itemprop="description" content="geek猫，中国科大14级，南七技校工科男一枚。不知名博主，擅长linux运维，爱好摄影，歌风民谣，专业力学">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ZhZhY的博客 | ZhZhY's">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          网页版 俄罗斯方块tetris
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2018-03-01 17:49:24" itemprop="dateCreated datePublished" datetime="2018-03-01T17:49:24+00:00">2018-03-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/old-blog/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB-vue-github/" itemprop="url" rel="index"><span itemprop="name">技术分享 - vue - github</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Disqus: </span>
    
    <a title="disqus" href="/old-blog/2018/03/01/tetris/#disqus_thread" itemprop="discussionUrl">
      <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/03/01/tetris/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="vue-tetris"><a href="#vue-tetris" class="headerlink" title="vue-tetris"></a>vue-tetris</h1><blockquote>
<p>使用Vue实现的俄罗斯方块游戏</p>
</blockquote>
<h2 id="Build-Setup"><a href="#Build-Setup" class="headerlink" title="Build Setup"></a>Build Setup</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># install dependencies</span></span><br><span class="line">npm install</span><br><span class="line"></span><br><span class="line"><span class="comment"># serve with hot reload at localhost:8080</span></span><br><span class="line">npm run dev</span><br><span class="line"></span><br><span class="line"><span class="comment"># build for production with minification</span></span><br><span class="line">npm run build</span><br></pre></td></tr></table></figure>

<p>For detailed explanation on how things work, consult the <a target="_blank" rel="noopener" href="http://vuejs.github.io/vue-loader">docs for vue-loader</a>.</p>
<h2 id="在线访问"><a href="#在线访问" class="headerlink" title="在线访问"></a>在线访问</h2><blockquote>
<p><a href="https://zhzhzhy.github.io/vue-tetris/">在线访问</a></p>
</blockquote>
<h2 id="项目源码"><a href="#项目源码" class="headerlink" title="项目源码"></a>项目源码</h2><p>转载自lewis-geek，万分感谢<br><a target="_blank" rel="noopener" href="https://github.com/lewis-geek/vue-tetris">vue-tetris</a></p>
<h2 id="游戏简介"><a href="#游戏简介" class="headerlink" title="游戏简介"></a>游戏简介</h2><p>实现俄罗斯方块基本规则外，游戏增加了4种不同的色彩区别方块、键盘以及屏幕操作、游戏音效、积分系统、<a target="_blank" rel="noopener" href="https://docs.wilddog.com/sync/Web/index.html">实时通信引擎</a>实现的玩家排行榜等。同时支持PC和移动端。</p>
<span id="more"></span>
<h2 id="操作介绍"><a href="#操作介绍" class="headerlink" title="操作介绍"></a>操作介绍</h2><h3 id="键盘操作"><a href="#键盘操作" class="headerlink" title="键盘操作"></a>键盘操作</h3><p>Z键:旋转<br>空格:暂停/显示排行榜<br>左箭头:向左移动<br>右箭头:向右移动<br>上箭头:下坠<br>下箭头:向下移动</p>
<h3 id="屏幕操作"><a href="#屏幕操作" class="headerlink" title="屏幕操作"></a>屏幕操作</h3><p>大按钮:旋转<br>PAUSE:暂停/显示排行榜<br>左按钮:向左移动<br>右按钮:向右移动<br>上按钮:下坠<br>下按钮:向下移动</p>
<h2 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h2><p>项目使用vue框架，使用到了vue的页面渲染、计算属性、watcher等功能。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">.</span><br><span class="line">├── README.md</span><br><span class="line">├── index.html          # 页面入口</span><br><span class="line">├── package.json        # npm相关</span><br><span class="line">├── src     </span><br><span class="line">│   ├── App.vue         # 主文件</span><br><span class="line">│   ├── assets          # 静态资源</span><br><span class="line">│   │   ├── clear.mov</span><br><span class="line">│   │   ├── down.mov</span><br><span class="line">│   │   └── over.mov</span><br><span class="line">│   └── main.js</span><br><span class="line">└── webpack.config.js   # webpack配置</span><br></pre></td></tr></table></figure>

<h2 id="实现思路"><a href="#实现思路" class="headerlink" title="实现思路"></a>实现思路</h2><h3 id="视图部分"><a href="#视图部分" class="headerlink" title="视图部分"></a>视图部分</h3><p>游戏区对应一个二维数组，可约定两个量表示当前坐标是否有方块，例如1表示有方块，0表示没有方块。为了丰富游戏体验，也可将两个量改为多个量，区别不同的方块模型，为方块模型设置不同颜色，提高游戏趣味性。   </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrColor = []</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> r = <span class="number">0</span>; r &lt; row; r++) &#123;</span><br><span class="line">      arrColor.push([])</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> c = <span class="number">0</span>; c &lt; col; c++) &#123;</span><br><span class="line">      arrColor[r].push(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="方块模型"><a href="#方块模型" class="headerlink" title="方块模型"></a>方块模型</h3><p>方块模型对应下图的几种形态，每个模型最多由4个小方块组成，将方块模型保存在4x4的二维数组中，使用6个不同的数组即可表示全部模型初始状态。  </p>
<p>我在组件中声明了一个shape属性，每次生成新的方块模型时，随机选择一个方块模型的二维数组，并赋值给shape。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (reandom) &#123;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">      <span class="built_in">this</span>.shape = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="http://olnzpdi2u.bkt.clouddn.com/3908048923-57dc200ce044c_articlex.png" alt="四格骨骼"></p>
<h3 id="旋转算法"><a href="#旋转算法" class="headerlink" title="旋转算法"></a>旋转算法</h3><p>通过旋转算法，配合6个初始的方块模型，即可表示全部的方块图案。将一个4x4的矩阵旋转90度，推导对应坐标的转换关系即可。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> rotatedShape = []   <span class="comment">//保存旋转后的方块模型坐标</span></span><br><span class="line"><span class="keyword">let</span> shape = <span class="built_in">this</span>.shape  <span class="comment">//取当前的方块模型坐标</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//循环遍历原方块模型二维数组，将旋转后的坐标保存在rotatedShape数组</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> r = <span class="number">0</span>; r &lt; shape.length; r++) &#123;</span><br><span class="line">      rotatedShape[r] = []</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> c = <span class="number">0</span>; c &lt; shape[r].length; c++) &#123;</span><br><span class="line">      <span class="comment">//坐标替换关系</span></span><br><span class="line">      rotatedShape[r][c] = shape[shape.length - c - <span class="number">1</span>][r]  </span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="碰撞算法"><a href="#碰撞算法" class="headerlink" title="碰撞算法"></a>碰撞算法</h3><p>当方块模型移动时进行碰撞检测，如果发生碰撞，则不能移动，需要碰撞检测算法。我的实现思路是，遍历移动后的方块模型坐标，在视图中检查对应坐标是否已有方块，同时要考虑边界，不能超出视图边界。</p>
<pre><code class="javascript">function colDetection(shapeCoord) &#123;
  let pass = true

  for (let coord of shapeCoord) &#123;
    if (coord.y &gt; this.rowNum - 1 || coord.y &lt; 0) &#123;
      return false
    &#125;
    if (coord.x &gt; this.colNum - 1 || coord.x &lt; 0) &#123;
      return false
    &#125;
    if (this.layout[coord.y][coord.x] == 1) &#123;
      pass = false
      for (let c of this.shapeCoord) &#123;
        if (coord.y == c.y &amp;&amp; coord.x == c.x) &#123;
          pass = true
        &#125;
      &#125;
      if (pass == false) &#123;
        return false
      &#125;
    &#125;
  &#125;
  return pass
&#125;
</code></pre>
<h3 id="移动和变形"><a href="#移动和变形" class="headerlink" title="移动和变形"></a>移动和变形</h3><p>定义一个坐标点为表示方块模型左上角顶点坐标，使用vue框架的计算属性，当方块模型或者顶点坐标发生变化时，自动计算新的视图。<br>方块模型改变时，需要两步操作，先清除当前在坐标中的坐标，再将新的坐标添加到视图中。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>以上只是实现俄罗斯方块游戏的几个关键点，要完美的实现俄罗斯方块游戏还需要很多相对复杂的逻辑代码，不具体介绍了。</p>
<blockquote>
<p>转载自lewis-geek，万分感谢</p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/lewis-geek/vue-tetris">https://github.com/lewis-geek/vue-tetris</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/old-blog/tags/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" rel="tag"># 技术分享</a>
              <a href="/old-blog/tags/vue/" rel="tag"># vue</a>
              <a href="/old-blog/tags/github/" rel="tag"># github</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/old-blog/2018/03/01/%E6%BA%90%E6%B0%8F%E7%89%A9%E8%AF%AD/" rel="prev" title="源氏物语">
                  <i class="fa fa-chevron-left"></i> 源氏物语
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/old-blog/2018/03/08/graduation_trip/food-in-thailand/" rel="next" title="泰国的那些美食">
                  泰国的那些美食 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    
  <div class="comments" id="disqus_thread">
    <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class=""></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhZhy</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/old-blog/js/utils.js"></script><script src="/old-blog/js/motion.js"></script><script src="/old-blog/js/next-boot.js"></script>

  






  




  <script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'none'
      },
      options: {
        renderActions: {
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              const target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-mml-chtml.js';
    script.defer = true;
    document.head.appendChild(script);
  } else {
    MathJax.startup.document.state(0);
    MathJax.typesetClear();
    MathJax.texReset();
    MathJax.typeset();
  }
</script>



<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://http-zhzhy-top.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  var disqus_config = function() {
    this.page.url = "https://zhzhzhy.github.io/old-blog/2018/03/01/tetris/";
    this.page.identifier = "2018/03/01/tetris/";
    this.page.title = "网页版 俄罗斯方块tetris";
    };
  NexT.utils.loadComments('#disqus_thread', () => {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://http-zhzhy-top.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  });
</script><!-- hexo-inject:begin --><!-- hexo-inject:end -->

</body>
</html>
